<?php

namespace app\store\controller;

use library\tools\Express;
use library\Controller;
use think\Db;

/**
 * 渠道数据
 * Class GoodsCate
 * @package app\store\controller
 */
class Channel extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    protected $table_DB_Platform_Account_statistics = 'db_platform_statistics'; /*平台账户配置库*/
    protected $user_recharge_info = 'recharge_info';                            /* 充值情况记录表 */
    protected $user_data_info = 'data_info';                                    /* 日活记录表 */
    protected function Db_connect_user_data_info()
    {
        $user_data_info = Db::connect($this->table_DB_Platform_Account_statistics)->table($this->user_data_info);
        return $user_data_info;
    }

    protected function Db_connect_user_recharge_info()
    {
        $user_recharge_info = Db::connect($this->table_DB_Platform_Account_statistics)->table($this->user_recharge_info);
        return $user_recharge_info;
    }

    /**
     * 商品分类管理
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->title = '渠道数据';
        $start_time = date('Y-m-d', time());
        $user_today_list = $this->Db_connect_user_data_info()->where($this->structure_where())->select();
        foreach ($user_today_list as $k => $v) {
            $channel_name = get_channel_name($v['channel_id']);
            $v['channel_name'] = $channel_name;
            $user_today_list[$k] = $v;
        }
        $this->assign('start_time', $start_time);
        $this->assign('user_today_list', $user_today_list);
        $this->assign('user_data_info', $this->user_data_info());
        $this->fetch();
    }

    /* 新增玩家 活跃玩家 玩家充值 三种渠道分布数据 */
    public function user_data_info()
    {
        $arr = $this->Db_connect_user_data_info()->fieldRaw('sum(newcount) as mynewcount,sum(logincount) as mylogincount,sum(recharge) as myrecharge,time,channel_id')->where($this->structure_where())->group('channel_id')->select();
        $this->totalJson = [];
        $arr1 = $arr2 = $arr3 = [];
        foreach ($arr as $key => $value) {
            $arr1[$key]['count'] = $value['mynewcount'];
            $arr2[$key]['count'] = $value['mylogincount'];
            $arr3[$key]['count'] = $value['myrecharge'];
            if ($value['channel_id'] == 0) {
                $arr1[$key]['channel_name'] = $arr2[$key]['channel_name'] = $arr3[$key]['channel_name'] = '官方-0-渠道';

            } else {
                $arr1[$key]['channel_name'] = $arr2[$key]['channel_name'] = $arr3[$key]['channel_name'] = get_channel_name($value['channel_id']);
            }
        }
        // 对数组按照count进行排序
        array_multisort(array_column($arr1, 'count'), SORT_DESC, $arr1);
        array_multisort(array_column($arr2, 'count'), SORT_DESC, $arr2);
        array_multisort(array_column($arr3, 'count'), SORT_DESC, $arr3);
        // 组合一维数组
        foreach ($arr1 as $value) {
            if ($value['count'] > 0) {
                $this->totalJson['mynewcount']['data'][] = $value['count'];
                $this->totalJson['mynewcount']['channel_name'][] = $value['channel_name'];
            }
        }
        foreach ($arr2 as $value) {
            if ($value['count'] > 0) {
                $this->totalJson['mylogincount']['data'][] = $value['count'];
                $this->totalJson['mylogincount']['channel_name'][] = $value['channel_name'];
            }
        }
        foreach ($arr3 as $value) {
            if ($value['count'] > 0) {
                $this->totalJson['myrecharge']['data'][] = $value['count'];
                $this->totalJson['myrecharge']['channel_name'][] = $value['channel_name'];
            }
        }
        return $this->totalJson;
    }

    /* 渠道充值分布情况 */
    public function user_recharge_info()
    {
        $arr = $this->Db_connect_user_recharge_info()->field('spreaderId,recharge')->where($this->structure_where())->select();
        $this->totalJson = ['spreaderId' => [], 'recharge' => []];
        foreach ($arr as $value) {
            $this->totalJson['recharge'][] = $value['recharge'];
            if ($value['spreaderId'] == 0) {
                $channel_name = '官方-0-渠道';
            } else {
                $channel_name = get_channel_name($value['spreaderId']);
            }
            $this->totalJson['spreaderId'][] = $channel_name;
        }
        return $this->totalJson;
    }

    protected function structure_where()
    {
        $search_time = $this->request->param('search_time');
        $where = [];
        if (!empty($search_time)) {
            $where[] = ['time', 'eq', $search_time];
        } else {
            $begin = date('Y-m-d', time());
            $where[] = ['time', 'eq', $begin];
        }
        return $where;
    }

    /**
     * 添加商品分类
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function add()
    {
        $this->title = '添加商品分类';
        $this->_form($this->table, 'form');
    }

    /**
     * 编辑商品分类
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit()
    {
        $this->title = '编辑商品分类';
        $this->_form($this->table, 'form');
    }

    /**
     * 禁用商品分类
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function forbid()
    {
        $this->_save($this->table, ['status' => '0']);
    }

    /**
     * 启用商品分类
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function resume()
    {
        $this->_save($this->table, ['status' => '1']);
    }

    /**
     * 删除商品分类
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function remove()
    {
        $this->_delete($this->table);
    }

}
